package com.micro.controller;

import com.micro.model.User;
import com.micro.quartz.ProductJob;
import com.micro.quartz.ProductJob2;
import com.micro.service.UserService;
import com.micro.util.Md5Util;
import com.micro.util.UUIDTool;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping("/prod")
public class ProductJobController {
	private ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:conf/spring.xml"
			,"classpath:conf/spring-mybatis.xml"});

	/**
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping("prod.do") //"value="qryInitData.do",method=RequestMethod.POST
	public void executePushProdPlan(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
		ProductJob productJob = new ProductJob();
		productJob.pushVersionPlan();

		response.getWriter().write("Pushed");
	}

	/**
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping("delayPlan.do") //"value="qryInitData.do",method=RequestMethod.POST
	public void executeDelayPushProdPlan(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
		ProductJob2 productJob = new ProductJob2();
		productJob.pushVersionPlan();

		response.getWriter().write("Pushed");
	}

	/**
	 * 查询用户是否存在 实名发布，实名回复  提交主题时候调用
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping("qryUserNameIsExist.do") //"value="qryInitData.do",method=RequestMethod.POST
	public void inValideUser(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, NoSuchAlgorithmException {
		User user = new User();
		user.setUserName(request.getParameter("USER_NAME"));
		user.setUserPwd(Md5Util.EncoderByMd5(request.getParameter("USER_PWD").toString()));
		UserService userService = (UserService) context.getBean("userServiceImpl");

		User resultUser = userService.queryUserByNameAndPwd(user);
		if ("1".equals(resultUser.getUserId())) {
			response.getWriter().write("YES");
		} else {
			response.getWriter().write("NO");
		}
	}

	/**
	 * 用户登录
	 * @param request
	 * @param response
	 * @param user
	 * @throws IOException
	 */
	@RequestMapping("logon.do")
	public void insertUser(HttpServletRequest request, HttpServletResponse response, User user) throws IOException, NoSuchAlgorithmException {
		UserService userService = (UserService) context.getBean("userServiceImpl");
//		HttpSession session = request.getSession();
		//先校验用户是否存在
		User resultUser = userService.queryUserByName(user);
		boolean userExist = false;
		if ("1".equals(resultUser.getUserId())) {
			userExist = true;
		}
		// 如果用户存在则校验密码
		if (userExist) {
			user.setUserPwd(Md5Util.EncoderByMd5(user.getUserPwd()));
			user = userService.queryUserByNameAndPwd(user);
			// 存在用户 且 密码正确
			if (null != user) {
//				session.setAttribute("user", user);
				// 10年有效
//				Cookie coo = new Cookie("userName", URLEncoder.encode(quser.getUserName(), "UTF-8"));
//				coo.setMaxAge(60*60*24*30*120);
//				response.addCookie(coo);
				response.getWriter().write("YES");
			}
			else {
				response.getWriter().write("NO");
			}
		}

		else {
			// 用户不存在则新建用户
			user.setUserId(UUIDTool.getUUID());
			user.setUserPwd(Md5Util.EncoderByMd5(user.getUserPwd()));
			int result = userService.insertUser(user);

			if(result == 1) {
//				session.setAttribute("user", user);
				// 10年有效
//				Cookie coo = new Cookie("userName", URLEncoder.encode(user.getUserName(), "UTF-8"));
//				coo.setMaxAge(60*60*24*30*120);
//				response.addCookie(coo);

//				response.getWriter().write(URLEncoder.encode(user.getUserName(), "UTF-8"));
				response.getWriter().write("YES");
			} else {
				response.getWriter().write("NO");
			}
		}
	}
}
